package com.sc.nft.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.sc.nft.exception.GlobalRunTimeException;
import org.web3j.abi.datatypes.Int;

import java.util.HashSet;
import java.util.Set;
public class SubscribeCodeUtil {
    public static final String staff = "abcdefghijklmnopqrstuvwsyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    /**
     * 获取多个不重复的申购码.
     *
     * @param num
     * @return
     */
    public static Set<String> subscribeCodeSet(Integer num) {
        Set set = new HashSet();
        while (set.size() < num) {
            set.add(StrUtil.format("{}",RandomUtil.randomString(staff, 10)));
        }
        return set;
    }

    /**
     * 获取一个申购码.
     *
     * @return
     */
    public static String subscribeCodeString() {
        return StrUtil.format("{}",RandomUtil.randomString(staff, 10));
    }

    /**
     * 排除掉set中的数据,在填充进对应数值量的码,生成一个新的码.
     *
     * @param exclude
     * @param set
     * @param num
     * @return
     */
    public static Set<String> subscribeCodeString(Set<String> exclude, Set<String> set, Integer num) {
        set.remove(exclude);
        for (int i = 0; i < num; i++) {
            set.add(StrUtil.format("{}",RandomUtil.randomString(staff, 10)));
        }
        return set;
    }

    /**
     * 删除掉排除的码数据,在填进去对应数量的码
     * 如果set中是10个,删除6个 那么会自己删除6个后再补6个
     * 如果exclude为空则返回set
     * 如果set为空依然返回set
     * @param exclude
     * @param set
     * @return
     */
    public static Set<String> subscribeCodeString(Set<String> exclude, Set<String> set) {
        if (CollUtil.isEmpty(exclude)) {
            return set;
        }
        if (CollUtil.isEmpty(set)) {
            return set;
        }
        Integer num = set.size();
        set.remove(exclude);
        while (set.size() < num) {
            set.add(StrUtil.format("{}",RandomUtil.randomString(staff, 10)));
        }
        return set;
    }
}
